﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TTQT;
using TTQT.TTQT_Code;

public partial class MucTin : MasterControl //System.Web.UI.UserControl
{
    public string formatIsUsed(string isUsed)
    {
        if (isUsed.Equals("1"))
            return "Có";
        else
            return "không";
    }
    public void bindGridCategory()
    {
        ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        ds.SelectCommand = "TinTuc_Nhom_GetAll";
        ds.SelectParameters.Clear();
        ds.SelectParameters.Add(new Parameter("IsUsed"));
        ds.SelectParameters["IsUsed"].DefaultValue = "-1";
        drgDataViewer.DataSource = (DataView)ds.Select(new DataSourceSelectArguments());
        drgDataViewer.DataBind();
        cboCategory.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindGridCategory();
        }
    }
    private void xoaFrom()
    {
        teDangMa.Text = "";
        teName.Text = "";
    }
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        if (teDangMa.Text.Length <= 0)
        {
            MyWebUtils.MyWebUtil.alert(this.Page, "Chưa nhập mã sắp xếp");
            return;
        }
        if (teName.Text.Length <= 0)
        {
           // MyWebUtils.MyWebUtil.alert(this.Page, "Chưa nhập tên");
           // return;
        }
        if (lblId.Text.Length == 0)//insert
        {
            ds.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
            ds.InsertCommand = "TinTuc_Nhom_Insert";
            ds.InsertParameters.Clear();
            ds.InsertParameters.Add(new Parameter("id"));
            ds.InsertParameters["id"].DefaultValue = lblId.Text;

            ds.InsertParameters.Add(new Parameter("dangma"));
            ds.InsertParameters["dangma"].DefaultValue = teDangMa.Text;

            ds.InsertParameters.Add(new Parameter("name"));
            ds.InsertParameters["name"].DefaultValue = teName.Text;

            ds.InsertParameters.Add(new Parameter("ParentId"));
            if (!cboCategory.SelectedValue.Equals(lblId.Text))
            { 
                ds.InsertParameters["ParentId"].DefaultValue = cboCategory.SelectedValue; 
            }
            else
            {
                ds.InsertParameters["ParentId"].DefaultValue = "0"; 
            }

            ds.InsertParameters.Add(new Parameter("IsUsed"));
            ds.InsertParameters["IsUsed"].DefaultValue = cboStatus.SelectedValue;
            ds.InsertParameters.Add(new Parameter("directLink"));
            ds.InsertParameters["directLink"].DefaultValue = txtDirectLink.Text;
            if (ds.Insert() > 0)
            {
                bindGridCategory();
                xoaFrom();
            }
            lblId.Text = "";
        }
        else //update
        {
            ds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
            ds.UpdateCommand = "TinTuc_Nhom_Update";
            ds.UpdateParameters.Clear();
            ds.UpdateParameters.Add(new Parameter("id"));
            ds.UpdateParameters["id"].DefaultValue = lblId.Text;

            ds.UpdateParameters.Add(new Parameter("dangma"));
            ds.UpdateParameters["dangma"].DefaultValue = teDangMa.Text;

            ds.UpdateParameters.Add(new Parameter("name"));
            ds.UpdateParameters["name"].DefaultValue = teName.Text;

            ds.UpdateParameters.Add(new Parameter("ParentId"));
            ds.UpdateParameters["ParentId"].DefaultValue = cboCategory.SelectedValue;

            //ds.UpdateParameters.Add(new Parameter("OrderNo"));
            //ds.UpdateParameters["OrderNo"].DefaultValue = cboCategory.SelectedValue;

            ds.UpdateParameters.Add(new Parameter("IsUsed"));
            ds.UpdateParameters["IsUsed"].DefaultValue = cboStatus.SelectedValue;
            ds.UpdateParameters.Add(new Parameter("directLink"));
            ds.UpdateParameters["directLink"].DefaultValue = txtDirectLink.Text;
            if (ds.Update() > 0)
            {
                bindGridCategory();
                xoaFrom();
            }
            lblId.Text = "";
        }

    }
    protected void drgDataViewer_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            ds.DeleteCommandType = SqlDataSourceCommandType.Text;
            ds.DeleteCommand = "Delete TinTuc_Nhom where id=" + e.Item.Cells[0].Text;
            if (ds.Delete() > 0)
            {
                //MyWebUtils.MyWebUtil.alert(Page, "Xoa thanh cong!");
                bindGridCategory();
            }
            else
            {
                MyWebUtils.MyWebUtil.alert(Page, "Khong xoa duoc!");
            }
        }
        if (e.CommandName.Equals("Edit"))
        {
            lblId.Text = e.Item.Cells[0].Text;
            teDangMa.Text = e.Item.Cells[1].Text;
            teName.Text = e.Item.Cells[2].Text;
            cboCategory.SelectedValue=e.Item.Cells[3].Text;
            cboStatus.SelectedValue = e.Item.Cells[4].Text;
            txtDirectLink.Text = e.Item.Cells[6].Text;


        }
    }
}
